package com.qf.controller;

import com.qf.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Rest风格开发
 *
 *     SpringMvc中：只要Path路径+请求方式 == 不一样 就可以写
 * @author lixu
 */
@Controller
public class RestController {

    /**
     * 添加用户
     *    @RequestMapping("") : 设置请求路径的  支持所有请求方式
     *      -- value = "" : 请求路径位置
     *
     *     //@RequestMapping(value = "/user",method = RequestMethod.POST)  只支持POST请求
     *
     *        用户对象
     *   添加
     *   http://localhost:8080/user/insertUser
     *   Rest风格
     *   http://localhost:8080/user    请求方式:POST
     *                       请求体中传递用户对象
     *                       <form  method="post">
     *                           <input name=
     *                           </form>
     */
    //@RequestMapping(value = "/user",method = RequestMethod.POST)
    @PostMapping(value = "/user")//简写
    public String insertUser(User user){
        //添加用户
        System.out.println("===添加用户===");
        System.out.println("user:" + user);
        return "hello";
    }

    /**
     修改
     http://localhost:8080/user/updateUser
     Rest风格
     http://localhost:8080/user    请求方式:PUT
     */
    //@RequestMapping(value = "/user")
    @PutMapping(value = "/user")//简写
    public String updateUser(User user){
        //修改用户
        System.out.println("===修改用户===");
        System.out.println("user:" + user);
        return "hello";//405 : Method not 允许   JSP页面只支持 GET POST DELETE 不支持PUT请求
    }

    /**
     * 查询用户
     *   查询
     *   http://localhost:8080/user/findUserById?id=123&name=小小
     *   Rest风格
     *   http://localhost:8080/user/123/小小    请求方式:GET
     */
    @GetMapping(value = "/user/{id}/{name}")
    public String findUserById(@PathVariable Integer id,@PathVariable String name){
        //查询用户
        System.out.println("===查询用户===");
        System.out.println("用户的ID:" + id + ":::用户的名称:" + name);
        return "hello";
    }
    /**
     *   删除
     *   http://localhost:8080/user/deleteUser?id=1
     *   Rest风格
     *   http://localhost:8080/user/1.html     请求方式:DELETE
     */
    @DeleteMapping("/user/{id}.html")
    public String deleteUser(@PathVariable Integer id){
        System.out.println("===删除用户===");
        System.out.println("用户的ID:" + id );
        return "hello";
    }


}
